package resp

import (
	"strings"
)

//响应json报文规约{00000:成功；11111:非预期异常；其它:业务异常}
type RespVo struct {
	//响应code
	Code string
	//响应描述
	Msg string
	//响应数据
	Data interface{}
}

//组装业务异常响应json报文
func BuildServicePanic(panicStr string) RespVo {
	resp := RespVo{}
	if strings.Contains(panicStr, "-") { //已定义业务异常{异常消息中包含"-"则认为是业务异常}
		resp.Code = strings.Split(panicStr, "-")[0]
		resp.Msg = strings.Split(panicStr, "-")[1]
	} else { //未定义业务异常：非预期异常，应该尽快排期修复
		resp.Code = "11111"
		resp.Msg = "服务繁忙"
	}
	return resp
}

//基础响应
func RespBase(result bool) RespVo {
	resp := RespVo{}
	if result {
		resp.Code = "00000"
		resp.Msg = "操作成功"
	} else {
		resp.Code = "11111"
		resp.Msg = "服务繁忙"
	}
	return resp
}

//带data业务数据响应
func RespData(data interface{}, code string, msg string) RespVo {
	resp := RespVo{Code: code, Msg: msg}
	if code == "" {
		resp.Code = "00000"
		resp.Msg = "操作成功"
	}
	resp.Data = data
	return resp
}
